package com.example.demo.controller;

import com.example.demo.model.Drink;
import com.example.demo.requests.ModifyDrinkRequest;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/drink")
public class DrinkController {



        @RequestMapping(value = "/",method = RequestMethod.GET)
    @GetMapping("/")//简写
    public List<Drink> getALL(){
        // 后期从数据库查询
        List<Drink> list = new ArrayList<>();

        list.add(Drink.builder().id(0).name("茉莉绿茶").price(Money.parse("USD 8.00")).build());
        list.add(Drink.builder().id(1).name("茉莉红茶").price(Money.parse("USD 8.00")).build());
        list.add(Drink.builder().id(2).name("茉莉蓝茶").price(Money.parse("USD 8.00")).build());

        return list;
    }

    @GetMapping("/{id}")
    public Drink getDrink(@PathVariable("id") Integer id){

        List<Drink> list = new ArrayList<>();

        Drink drink = new Drink();

        list.add(Drink.builder().id(0).name("茉莉绿茶").price(Money.parse("USD 8.00")).build());
        list.add(Drink.builder().id(1).name("茉莉红茶").price(Money.parse("USD 8.00")).build());
        list.add(Drink.builder().id(2).name("茉莉蓝茶").price(Money.parse("USD 8.00")).build());

        if (id >= list.size()){
            return drink;
        }else {
            drink = list.get(id);
            return drink;
        }
    }

    @PostMapping("/")
    public Drink addNewDrink(@RequestBody Drink drink){
        log.info("新增的: " + drink);

        Drink drink1 = Drink.builder().id(3).name("好茶").
                price(Money.parse("USD 8.00"))
                .build();


        return drink1;
    }

    @PutMapping("/")
    public Drink modifyDrink(@RequestBody ModifyDrinkRequest request){

        log.info("要修改的: " + request);

        Drink drink1 = Drink.builder().id(3).name("好茶").price(Money.parse("USD 8.00")).build();

        return drink1;
    }

    @DeleteMapping("/{id}")
    public String deleteDrink(@PathVariable("id") String id){
        log.info("删除: " + id);
        return "删除成功";
    }




    @PostMapping(value = "/batch",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public List<Drink> batchAddNewDrinks(MultipartFile file){
        List<Drink> results = new ArrayList<>();
        try {
//            InputStream inputStream = file.getInputStream();
//            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
//            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
            String line = "";
            while ((line = reader.readLine())!=null){

                String[] args = line.split(",");
                results.add(Drink.builder().id(Integer.parseInt(args[0]))
                        .name(args[0])
                        .price(Money.of(CurrencyUnit.of("CNY"),Double.parseDouble(args[2])))
                        .build());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return results;
    }
}
